﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using moose.extension;

namespace moose.irc.standard
{
    /// <summary>
    /// Register to IRC network [[((Moose Priorietary)), Register is not an IRC Server command, this is a Moose Alias]]
    /// </summary>
    [CLSCompliant(true)]
    [Command(
        Name = "moose-cmd-register",
        Description = "Request Core connect to IRC server",
        Author = "Mike MacKenzie-Jamer <email: mikejamer@gmail.com>",
        Version = "0.1")]
    [Trigger(TriggerText = "REGISTER")]
    public sealed class RegisterCommand : IrcAction
    {
        protected override IrcState valid_states()
        {
            return IrcState.Connected;
        }

        protected override void action(IrcClient client, IrcMessage message)
        {

            string[] args = message.Arguments.ToArray();
            if (args.Length > 0)
            {
                if (args.Length != 3)
                {
                    write_error(client);
                    return;
                }
                else
                {
                    client.ClientSettings[IrcClientSettings.NICKNAME] = args[0];
                    client.ClientSettings[IrcClientSettings.USER_NAME] = args[1];
                    client.ClientSettings[IrcClientSettings.REAL_NAME] = args[2];
                }
            }

            if ((!client.ClientSettings.ContainsKey(IrcClientSettings.NICKNAME)) ||
                (!client.ClientSettings.ContainsKey(IrcClientSettings.USER_NAME)) ||
                (!client.ClientSettings.ContainsKey(IrcClientSettings.REAL_NAME)))
            {
                write_error(client);
            }

            client.send("NICK " + client.ClientSettings[IrcClientSettings.NICKNAME]);
            client.send("USER " + client.ClientSettings[IrcClientSettings.USER_NAME] + " 8 *  :" + client.ClientSettings[IrcClientSettings.REAL_NAME]);
            return;
        }

        private void write_error(IrcClient client) {

            client.echo("-REGISTER- Unable to register, must register manually..");
            client.echo("-REGISTER- /NICK <desired nickname>");
            client.echo("-REGISTER- /USER <desired username> 8 * :<desird real name>");
            return;
        }
    }
}
